package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.query.CustomerTransferQueryObject;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.ICustomerTransferService;
import cn.wolfcode.crm.service.impl.CustomerTransferServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Date;

//类似之前的Servlet
//请求用户的请求和相应

@Controller//IOC注解，将Controller交给Spring容器管理
@RequestMapping("/customerTransfer")
public class CustomerTransferController {

    //自动去Spring容器中找到对应类型或者名称的对象并设置给当前字段
    @Autowired
    private ICustomerTransferService service = new CustomerTransferServiceImpl();

    //处理查询的请求
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerTransferQueryObject qo) {
        //查询到所有的部门
        PageResult result = service.query(qo);
        //将查询到的部门共享给页面
        model.addAttribute("result", result);

        //指定页面跳转的视图
        return "customerTransfer/list";
    }


    //执行保存或更新
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(CustomerTransfer transfer, HttpSession session) {
        Employee employee = (Employee) session.getAttribute("EMPLOYEE_IN_SESSION");
        transfer.setOperator(employee);

        transfer.setOperateTime(new Date());


        service.save(transfer);
        return "redirect:/customerTransfer/list.do";
    }
}
